package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;

    @RequestMapping("/add")
    public Result addAddress(@RequestBody Address adds) {
        try {
            System.out.println("adds = " + adds);
            addressService.addAddress(adds);
            return new Result(true, "地址添加成功! ");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "地址添加失败! ");
        }
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pr = addressService.findPage(queryPageBean);
            return new Result(true, "地址查询成功! ", pr);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "地址查询失败! ");
        }
    }

    @RequestMapping("/allAddress")
    public List<String> allAddress() {
        List<String> address = addressService.allAddress();
        return address;
    }

    @RequestMapping("/deleteAddress")
    public Result deleteAddress(int id) {
        try {
            addressService.deleteAddress(id);
            return new Result(true, "地址删除成功! ");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "地址删除失败! ");
        }
    }
}
